<template>
  <el-dialog
    :append-to-body="true"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :title="globalData.title"
    :visible.sync="isDialog.visible"
    top="10vh"
    width="600px"
  >
    <el-form
      ref="submitFormRef"
      :model="submitForm"
      :rules="submitFormRules"
      label-position="right"
      label-width="auto"
    >
      <el-form-item label="选择区域" prop="region_id">
        <el-select
          v-model="submitForm.region_id"
          :loading="searchItem.region_id.loading"
          :remote-method="handleSearchRegion"
          clearable
          filterable
          placeholder="选择所在城市"
          remote
          reserve-keyword
        >
          <el-option
            v-for="item in searchItem.region_id.options"
            :key="item.region_id"
            :label="item.name"
            :value="item.region_id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="关键词" prop="keywords">
        <el-input
          v-model="submitForm.keywords"
          clearable
          placeholder="请输入关键词"
          type="text"
        >
        </el-input>
      </el-form-item>
    </el-form>

    <div slot="footer">
      <el-button @click="handleClose">取消</el-button>
      <el-button
        :loading="isLoading.submit"
        type="primary"
        @click="handleSubmit"
      >
        确 定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add } from "@/api/keywords";
import { lists as searchRegion } from "@/api/region";

export default {
  name: "Edit",
  props: {
    info: {
      type: Object,
      require: true,
    },
  },
  data() {
    return {
      // 表单提交
      submitForm: {
        region_id: "",
        keywords: "",
      },
      // 表单验证
      submitFormRules: {
        region_id: [{ required: true, message: "请选择区域", trigger: "blur" }],
        keywords: [
          { required: true, message: "请输入关键词", trigger: "blur" },
        ],
      },
      globalData: {
        title: "",
      },
      // 远程搜索项
      searchItem: {
        region_id: {
          loading: false,
          options: [],
        },
      },
      // 加载状态
      isLoading: {
        submit: false,
      },
      // 是否弹窗
      isDialog: {
        visible: true,
      },
    };
  },
  created() {
    this.globalData.title = "添加";
    this.handleSearchRegion();
  },
  methods: {
    // 获取城市
    handleSearchRegion(query) {
      this.searchItem.region_id.loading = true;
      searchRegion({
        page: 1,
        rows: 10,
        keywords: query ? query : "",
      }).then((res) => {
        this.searchItem.region_id.loading = false;
        this.searchItem.region_id.options = res.data.list;
      });
    },
    // 提交
    handleSubmit() {
      this.$refs.submitFormRef.validate((valid) => {
        if (!valid) {
          return false;
        }
        this.isLoading.submit = true;

        add(this.submitForm)
          .then((err) => {
            this.isLoading.submit = false;
            this.$emit("close");
          })
          .catch(() => {})
          .finally(() => {
            this.isLoading.submit = false;
          });
      });
    },
    // 关闭
    handleClose() {
      this.$emit("close", "cancel");
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-textarea textarea {
  font-family: "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
}
</style>
